//
//  ZZTalkListCell.m
//  QQplayground
//
//  Created by zhangxy on 2018/7/20.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZTalkListCell.h"

#import <SDWebImage/UIImageView+WebCache.h>
#import "ZZShareView.h"
#import "MWPhotoBrowser.h"
#import "UserInfoController.h"
@implementation ZZTalkListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _imgAvatar.layer.cornerRadius = 22.0f;
    _imgAvatar.layer.masksToBounds = YES;
    
    [_labTitle setFont:FontFiftent];
    _labTitle.numberOfLines = 0;
    [_labTitle setTextColor:UIColorFromRGB(TextSizeOneColor)];
    
    
    [_labName setFont:FontFiftent];
    [_labName setTextColor:UIColorFromRGB(TextSizeOneColor)];
    
    
    [_btnAttent.titleLabel setFont:FontFiftent];
    _btnAttent.layer.cornerRadius = 3.0f;
    _btnAttent.layer.masksToBounds = YES;
    [_btnAttent setTitleColor:UIColorFromRGB(TextSizeEightColor) forState:0];
    [_btnAttent setBackgroundColor:UIColor.clearColor];
    [_btnAttent setImage:[UIImage imageNamed:@"讨论组 举报"] forState:0];
    [_btnAttent setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_btnAttent addTarget:self action:@selector(onCotrolClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [_btnAttent setTitle:@"举报" forState:0];
    
    [_labType setFont:FontThirteen];
    [_labType setTextColor:UIColorFromRGB(BgTitleColor)];
    
    [_labTime setFont:FontThirteen];
    [_labTime setTextColor:UIColorFromRGB(BgLineColor)];
    
    [self setBtnStype:_btnFav image:@"赞"];
    [self setBtnStype:_btnComment image:@"评论 小按钮"];
    [self setBtnStype:_btnStar image:@"收藏 小按钮"];
    [self setBtnStype:_btnShare image:@"icon_sharenew"];
    
    [_btnShare setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_btnShare setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    
    _viewContent.userInteractionEnabled = YES;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    _labType.hidden = YES;
    
    [_btnLevel.titleLabel setFont:ListElevenFont];
    _btnLevel.layer.cornerRadius = 4.0f;
    _btnLevel.layer.masksToBounds = YES;
    _btnLevel.userInteractionEnabled = NO;
    _btnVip.userInteractionEnabled = NO;
    [_btnLevel setTitleColor:UIColorFromRGB(TextSizeOneColor) forState:0];
    
    _imgAvatar.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
    [_imgAvatar addGestureRecognizer:tap1];
    
    _labName.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
    [_labName addGestureRecognizer:tap2];
    
    
    [_btnDel setImage:[UIImage imageNamed:@"发布 关闭 标签"] forState:0];
    [_btnDel setTitle:@"" forState:0];
    _btnDel.hidden = YES;
    [_btnDel addTarget:self action:@selector(onCotrolClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)setBtnStype:(UIButton *) btn image:(NSString *)imgName{
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [btn setTitleColor:UIColorFromRGB(TextSizeOneColor) forState:0];
    [btn.titleLabel setFont:ListElevenFont];
    [btn setTitle:@"" forState:0];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 2, 0, 0)];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn setBackgroundColor:UIColor.clearColor];
    [btn addTarget:self action:@selector(onCotrolClick:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.userInteractionEnabled = YES;
}

-(void)onCotrolClick:(UIButton *)btn{
    ZZTopicCellClickType type = 0;
    if([btn isEqual:_btnShare]){
        type = ZZClickShare;
    }else if([btn isEqual:_btnFav]){
        type = ZZClickFav;
    }else if([btn isEqual:_btnStar]){
        type = ZZClickStar;
    }else if([btn isEqual:_btnComment]){
        type = ZZClickComment;
    }else if([btn isEqual:_btnAttent]){
//        type = ZZClickAttentUser;
        type = ZZClickReport;
    }
    else if([btn isEqual:_btnDel]){
        //        type = ZZClickAttentUser;
        type = ZZClickDel;
    }
    
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:type obj:self.tempModel];
    }
}


-(void) moreComment:(UITapGestureRecognizer *) tap{
    
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:ZZClickComment obj:self.tempModel];
    }
}


-(void)dataToView:(ForumEntity *) topic{
    self.tempModel = topic;
    self.backgroundColor = UIColor.clearColor;
    [self setUserViewData:topic];
    [_imgBgView setBackgroundColor:UIColor.whiteColor];
    _imgBgView.layer.cornerRadius = 10.0f;
    _imgBgView.layer.masksToBounds = YES;
    
    CGRect uf = _imgBgView.frame;
    uf.size.width = ScreenWidth;
    uf.size.height = CGRectGetMaxY(_viewControl.frame) + 10;
    _imgBgView.frame = uf;
    
//    if([topic.user_info.user_relation intValue] == 1){
//        [self.btnAttent setTitle:@"取消" forState:0];
//    }else{
//        [self.btnAttent setTitle:@"关注" forState:0];
//    }
    
    self.frame = CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(_imgBgView.frame)+10);
}

-(void)setUserViewData:(ForumEntity *) topic{
    [_imgAvatar sd_setImageWithURL:[NSURL URLWithString:convertToString(topic.user_info.user_avatar)] placeholderImage:[UIImage imageNamed:@"default_image"]];
    [_labName setText:topic.user_info.user_name];
    [_labName sizeToFit];
    [_labTime setText:topic.last_user_time];
    
#warning - 显示HTML文本在label
    NSString *labStr = [NSString stringWithFormat:@"%@ <br/> %@", topic.title, topic.message];
    NSMutableAttributedString *labTitle = [[NSMutableAttributedString alloc] initWithData:[labStr dataUsingEncoding:NSUnicodeStringEncoding]
                                                                           options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
                                                                documentAttributes:nil
                                                                             error:nil];
    [labTitle addAttribute:NSFontAttributeName value:FontFiftent range:NSMakeRange(0, labTitle.length)];
    [labTitle addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(TextSizeOneColor) range:NSMakeRange(0, labTitle.length)];
    _labTitle.attributedText = labTitle;
    
    CGSize st = [self autoWidthOfLabelHegith:_labTitle with:ScreenWidth - 40];
    [self autoWidthOfLabel:_labName with:21];
    CGRect lf = _btnLevel.frame;
    lf.origin.x = CGRectGetMaxX(_labName.frame) + 5;
    _btnLevel.frame = lf;
    //设置用户等级显示
    [self setBtnLevelColor:topic];
    
    if ([topic.user_info.is_vip intValue] == 1){
        _btnVip.hidden = NO;
        [_btnVip setImage:[UIImage imageNamed:@"vip"] forState:0];
        [_labName setTextColor:[UIColor colorWithHexString:@"#FF70A6" alpha:1.0]];
    }else{
        _btnVip.hidden = YES;
        [_labName setTextColor:UIColorFromRGB(TextSizeOneColor)];
    }
    
    if(topic.images.count == 0){
        [_viewContent.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        CGRect contentF = _viewContent.frame;
        contentF.origin.y = CGRectGetMaxY(_labTitle.frame) + ((st.height>0)?10:0);
        contentF.size.height = 1;
        [_viewContent setFrame:contentF];
    }else{
        [self addContontImages:topic fy:CGRectGetMaxY(_labTitle.frame) + ((st.height>0)?10:0)];
    }
    
    
    
//    CGRect typeF = _labType.frame;
//    typeF.origin.y = CGRectGetMaxY(_viewContent.frame) + 10;
//    _labType.frame = typeF;
//
//    [_labType setText:@"没有"];
    
    
    [_btnFav setTitle:topic.count_like forState:0];
    [_btnComment setTitle:topic.count_post forState:0];
    [_btnStar setTitle:topic.count_fav forState:0];
    
    if([topic.is_like isEqualToString:@"1"]){
        [_btnFav setImage:[UIImage imageNamed:@"赞 copy"] forState:0];
    }else{
        [_btnFav setImage:[UIImage imageNamed:@"赞"] forState:0];
    }
    
    if([topic.is_fav isEqualToString:@"1"]){
        [_btnStar setImage:[UIImage imageNamed:@"收藏 小按钮 copy"] forState:0];
    }else{
        [_btnStar setImage:[UIImage imageNamed:@"收藏 小按钮"] forState:0];
    }
    
//    CGRect bf1 = [self sizeForNoticeBtnTitle:_btnFav];
//    CGRect bf2 = [self sizeForNoticeBtnTitle:_btnComment];
//    CGRect bf3 = [self sizeForNoticeBtnTitle:_btnStar];
//
//    bf2.origin.x = CGRectGetMaxX(bf1) + 5;
//    _btnComment.frame = bf2;
//
//    bf3.origin.x = CGRectGetMaxX(bf2) + 5;
//    _btnStar.frame = bf3;
    
    CGRect cf = _viewControl.frame;
    cf.origin.y = CGRectGetMaxY(_viewContent.frame) + 10;
    _viewControl.frame = cf;
}
-(void)avatarClick:(UITapGestureRecognizer *) tap{
    UserInfoController *vc = [[UserInfoController alloc] init];
    vc.user_id = _tempModel.user_info.user_id;
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:ZZClickOpenVC obj:vc];
    }
}

-(void)addContontImages:(ForumEntity *)topic fy:(CGFloat ) y{
    [_viewContent.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat maxWidth = ScreenWidth - 40;
    CGFloat space = 6;
    CGFloat minWidth = (ScreenWidth - 40 - space*2)/3;
    
    NSMutableArray *imgsObj = topic.images;
    if(imgsObj.count == 1){
        ImageEntity *firstObj = [imgsObj firstObject];
        UIImageView *imgView = [self createImageView];
        imgView.tag = 0;
        [imgView setFrame:CGRectMake(0, 0, maxWidth, maxWidth)];
        [imgView sd_setImageWithURL:[NSURL URLWithString:firstObj.image_url] placeholderImage:[UIImage imageNamed:@"default_image"]];
        
        imgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [tap addTarget:self action:@selector(imgTouchUpInside:)];
        [imgView addGestureRecognizer:tap];
        
        [_viewContent addSubview:imgView];
    }else{
        CGFloat x = 0;
        
        for (int i = 0;i<imgsObj.count; i ++ ) {
            if(i==6){
                break;
            }
            
            if(i==4 && [topic.nav_type isEqual:@"pic"]){
                break;
            }
            ImageEntity *firstObj = imgsObj[i];
            UIImageView *imgView = [self createImageView];
            [imgView sd_setImageWithURL:[NSURL URLWithString:firstObj.image_url] placeholderImage:[UIImage imageNamed:@"default_image"]];
            imgView.layer.cornerRadius = 3.0f;
            imgView.layer.masksToBounds = YES;
            imgView.tag = i;
            
            imgView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            [tap addTarget:self action:@selector(imgTouchUpInside:)];
            [imgView addGestureRecognizer:tap];
            
            // 张图片居中
            if(imgsObj.count == 2){
                minWidth = (maxWidth - space)/2;
                x = (maxWidth - minWidth * 2 - space * 2)/2;
                
                if(i==0){
                    [imgView setFrame:CGRectMake(x, 0, minWidth, minWidth)];
                }else{
                    [imgView setFrame:CGRectMake(x + space + minWidth, 0, minWidth, minWidth)];
                }
            }else if(imgsObj.count == 4 || [topic.nav_type isEqual:@"pic"]){
                // 张图片，第一个放大，其它居中
                if(i==0){
                    [imgView setFrame:CGRectMake(x, 0, maxWidth, 216)];
                }else{
                    [imgView setFrame:CGRectMake(x, 222, minWidth, minWidth)];
                    x = minWidth + x + space;
                }
            }else{
                // 换行，3 、4、6情况
                if(i <= 2){
                    [imgView setFrame:CGRectMake(x, 0, minWidth, minWidth)];
                }else{
                    [imgView setFrame:CGRectMake(x, minWidth + 6, minWidth, minWidth)];
                }
                
                //换行
                if(i == 2){
                    x = 0;
                }else{
                    x = minWidth + x + space;
                }
            }
            [_viewContent addSubview:imgView];
            if((imgsObj.count > 6 && i == 5) || (imgsObj.count > 4 && i==3 && [topic.nav_type isEqual:@"pic"])){
                
                UILabel *label =  [self createLabel];
                [label setFrame:imgView.frame];
                [label setTextAlignment:NSTextAlignmentCenter];
                [label setTextColor:UIColorFromRGB(TextWhiteColor)];
                if([topic.nav_type isEqual:@"pic"]){
                    [label setText:[NSString stringWithFormat:@"%zdP",imgsObj.count - 4]];
                }else{
                    [label setText:[NSString stringWithFormat:@"%zdP",imgsObj.count - 6]];
                }
                [_viewContent addSubview:label];
            }
        }
    }
    
    CGRect f = _viewContent.frame;
    f.origin.y = y;
    if(imgsObj.count == 1){
        f.size.height = maxWidth;
   }else if(imgsObj.count == 4 || (imgsObj.count > 4 && [topic.nav_type isEqual:@"pic"])){
        f.size.height = 216 + space + minWidth;
    }else{
        if(imgsObj.count <= 3){
            f.size.height = minWidth;
        }else{
            f.size.height = minWidth * 2 + space;
        }
    }
    [_viewContent setFrame:f];
}



-(UIImageView *)createImageView{
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView setContentMode:UIViewContentModeScaleAspectFill];
    imgView.clipsToBounds = YES;
    return imgView;
}

-(UILabel *) createLabel{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextAlignment:NSTextAlignmentLeft];
    [label setFont:FontThirteen];
    [label setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [label setBackgroundColor:UIColor.clearColor];
    [label setNumberOfLines:0];
    return label;
}

/**
 计算Label高度
 
 @param label 要计算的label，设置了值
 @param width label的最大宽度
 */
- (CGSize )autoWidthOfLabel:(UILabel *)label with:(CGFloat )height{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(FLT_MAX, height);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.width = expectedLabelSize.width;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}

- (CGSize )autoWidthOfLabelHegith:(UILabel *)label with:(CGFloat )width{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.height = expectedLabelSize.height;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}



/**
 *  点击查看大图
 *
 *  @param
 */
-(void) imgTouchUpInside:(UITapGestureRecognizer *)recognizer{
    DLog(@"我怎么不处罚呢：");
    UIImageView *_picView = (UIImageView*)recognizer.view;
    NSMutableArray *photos = [NSMutableArray new];
    for (int i = 0;i<self.tempModel.images.count; i ++ ) {
        ImageEntity *firstObj = self.tempModel.images[i];
        
        MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:firstObj.origin_image]];
        
        [photos addObject:photo];
    }
    
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithPhotos:photos];
    [browser setCurrentPhotoIndex:_picView.tag];
    browser.zoomPhotosToFill = YES;
    browser.displayNavArrows = YES;
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:ZZClickOpenVC obj:browser];
    }
    //
    //    CALayer *calayer = _picView.layer.mask;
    //    [_picView.layer.mask removeFromSuperlayer];
    //
    //
    //    NSMutableArray *photos = [[NSMutableArray alloc] init];
    //
    //    XHImageViewer *xh = [[XHImageViewer alloc] initWithImageViewerWillDismissWithSelectedViewBlock:^(XHImageViewer *imageViewer, UIImageView *selectedView) {
    //
    //    } didDismissWithSelectedViewBlock:^(XHImageViewer *imageViewer, UIImageView *selectedView) {
    //
    //        selectedView.layer.mask = calayer;
    //        [selectedView setNeedsDisplay];
    //
    //
    //        [photos removeAllObjects];
    //    } didChangeToImageViewBlock:^(XHImageViewer *imageViewer, UIImageView *selectedView) {
    //
    //    }];
    //
    ////    for (int i = 0;i<self.tempModel.images.count; i ++ ) {
    //////        if(i==_picView.tag){
    //////            [photos addObject:_picView];
    //////        }else{
    ////            ImageEntity *firstObj = self.tempModel.images[i];
    ////            UIImageView *imgView = [self createImageView];
    ////            imgView.tag = i;
    ////            [imgView setFrame:_picView.frame];
    ////            [imgView sd_setImageWithURL:[NSURL URLWithString:firstObj.image_url] placeholderImage:[UIImage imageNamed:@"default_image"]];
    ////            [photos addObject:imgView];
    //////        }
    ////
    ////    }
    //    [photos addObjectsFromArray:_viewContent.subviews];
    //
    //    //    xh.delegate = self;
    //    xh.disableTouchDismiss = NO;
    //    [xh showWithImageViews:photos selectedView:_picView];
}


/**
 *  字符串获取属性
 *  @param text
 *  @param font
 *
 *  @return size
 */
//- (CGRect )sizeForNoticeBtnTitle:(UIButton *)btn{
//    CGSize tempSize = [self autoWidthOfLabel:btn.titleLabel with:CGFLOAT_MAX];
//
//    CGRect self_Rect = btn.frame;
//    self_Rect.size.width = tempSize.width + 20;
//    btn.frame = self_Rect;
//
//    return self_Rect;
//}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//用户等级数的显示问题
-(void)setBtnLevelColor:(ForumEntity *) topic {
    int level = [topic.user_info.group_icon intValue];
    if (level >= 100){
        CGRect lf = _btnLevel.frame;
        lf.size.width = 60;
        _btnLevel.frame = lf;
        [_btnVip setFrame:CGRectMake(CGRectGetMaxX(lf) + 2, lf.origin.y - 2, 52, 18)];
        if (level == 100){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"荣誉会员"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"荣誉会员"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level == 101){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"官方团队"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"官方团队"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level == 403){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"1"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"系统屏蔽"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"系统屏蔽"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#A4A4A4" alpha:1.0] forState:0];
        } else if (level == 102){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"小组长"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"小组长"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level == 200){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"原创达人"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"原创达人"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#8D6FFF" alpha:1.0] forState:0];
        } else if (level == 201){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"专题小编"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"专题小编"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#8D6FFF" alpha:1.0] forState:0];
        }
    }
    
    else {
        //等级显示处理
        CGRect lf = _btnLevel.frame;
        [self autoWidthOfLabel:_labName with:21];
        lf.origin.x = CGRectGetMaxX(_labName.frame) + 2;
        lf.size.width = 46;
        _btnLevel.frame = lf;
        [_btnVip setFrame:CGRectMake(CGRectGetMaxX(lf) + 5, lf.origin.y - 2, 52, 18)];
        if (level >= 1 && level < 6){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"1"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 1"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv 0%@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#A4A4A4" alpha:1.0] forState:0];
        } else if (level >= 6 && level < 11) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"2"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 2"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#1BC156" alpha:1.0] forState:0];
        } else if (level >= 11 && level < 16) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"3"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 3"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#4A90E2" alpha:1.0] forState:0];
        } else if (level >= 16 && level < 21) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 4"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#8D6FFF" alpha:1.0] forState:0];
        } else if (level >= 21 && level < 26) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"5"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 5"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF9300" alpha:1.0] forState:0];
        } else if (level >= 26 && level < 31) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"5"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 6"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF9300" alpha:1.0] forState:0];
        } else if (level >= 31 && level < 36) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"7"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 7"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF70A6" alpha:1.0] forState:0];
        } else if (level >= 36 && level < 41) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"8"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 8"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#3785FF" alpha:1.0] forState:0];
        } else if (level >= 41 && level < 46) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 9"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#962BFF" alpha:1.0] forState:0];
        } else if (level >= 46 && level < 51) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 10"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level >= 51 && level < 56) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"11"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 11"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF70A6" alpha:1.0] forState:0];
        } else if (level >= 56 && level < 100) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"12"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 12"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFFFFF" alpha:1.0] forState:0];
        }
    }
    
}

@end
